package com.cym.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cym.service.DatabaseService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;

@Controller
@RequestMapping("/jsonAnalysis")
public class JsonAnalysisController extends BaseController {

	@Autowired
	DatabaseService databaseService;

	@RequestMapping("")
	public ModelAndView main(ModelAndView modelAndView, String keyword) {

		modelAndView.addObject("fromBase", databaseService.getByType(null));

		modelAndView.setViewName("/jsonAnalysis/index");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/build")
	public JsonResult getCode(String fromBaseId, String fromTable, String tmpTable, String toTable, String jsonColumn, String jsonAnalysis) {

		String sql = databaseService.buildJsonSql(fromTable, jsonColumn, jsonAnalysis);

		String sqlInsert = databaseService.buildJsonSqlInsert(tmpTable, toTable, jsonAnalysis);

		String sqlCreate = databaseService.buildJsonSqlCreate(toTable, jsonAnalysis);
		
		String sqlDel = databaseService.buildSqlDel(tmpTable);
		
		Map map = new HashMap();
		map.put("sql", sql);
		map.put("sqlInsert", sqlInsert);
		map.put("sqlCreate", sqlCreate);
		map.put("sqlDel", sqlDel);
		
		return renderSuccess(map);
	}

}
